﻿/******************************************************************
 *Company: http://www.xiaomutech.com/
 *fileName : %{Cpp:License:FileName} --- %{Cpp:License:ClassName}
 *Auth       : yhni (QQ:393320854)
 *Create    : 2022/1/24
 *Description   :
 *Histroy:
 *<Auth>    <Date>        <Ver>        <Content>
 *         2022/1/24
 *******************************************************************/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QTimer>
#include <QTcpSocket>
#include <QPropertyAnimation>

#include "qcardarea.h"
#include "alarmclock.h"
#include "animationwidget.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(QPoint cardpos WRITE setPos READ cardPos)

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    QByteArray getPackHeader(quint8 type, int len = 0x00);
    void transToSocket(const QByteArray &_data);
    void parseBuffer(const QByteArray &_data);
    void startPickCard(quint16 id, quint8 order, CardType _type);
    QPixmap makePixmap(CardType _type);

    void setPos(const QPoint &_pos);
    QPoint cardPos() const;

private slots:
    void sltPickCard();
    void sltPickLord();

    void sltPlayCards(HandType _type, CardList _cards);
    void rightPlayCars(HandType _type, CardList _cards);
    void leftPlayCars(HandType _type, CardList _cards);

    void on_btnStart_clicked();
    void on_btnThreeScroe_clicked();
    void on_btnZeroScore_clicked();
    void sltSocketConneced();
    void sltSockeDisconneced();
    void sltSocketRead();
    void sltAlarmTimeout();

private:
    Ui::MainWindow *ui;
    QTimer         *m_timer;
    int             m_cardIndex;
    CardList        m_cards;
    QMap<CardType, QPixmap> m_cardPix;
    AlarmClock     *m_alarmClock;

    QTcpSocket     *m_tcpClient;

    int             m_cardSize;
    QPoint          m_cardPos;
    QPropertyAnimation *m_animation;
    AnimationWidget *m_animationWidget;
private:
    void initCardPixs();
    void initCards();

protected:
    void resizeEvent(QResizeEvent *e) override;
    void paintEvent(QPaintEvent *) override;
    void mouseReleaseEvent(QMouseEvent *e) override;
    void keyPressEvent(QKeyEvent *e) override;
};
#endif // MAINWINDOW_H
